<?php

class UserPhoneModel extends BaseModel {
	/**
	 * 获取用户手机验证信息
	 * @param int $uid 用户标识
	 * @param int $phone 手机号码
	 * @param int $status 状态
	 * @param int $type 类型
	 * @return array
	 * @author Ice <iceinto@mallog.com.cn>
	 */
	public function getUidPhoneVerify($uid, $phone, $status = 1, $isValid = false, $type = 1, $id = 0){
		$map = array();
		$map['uid'] = intval($uid);
		$map['phone'] = $phone;
		$map['status'] = $status;
		$map['type'] = $type;
		$map['id'] = $id;
		if($isValid){
			$time = time() - 300;//五分钟有效
			$map['create_time'] = array('gt',$time);
		}
		return $this->where($map)->find();
	}

	/**
	 * 添加手机验证记录
	 * @param int $uid 用户标识
	 * @param int $phone 手机号码
	 * @param string $verify 验证码
	 * @return int
	 * @author Ice <iceinto@mallog.com.cn>
	 */
	public function addUidPhoneVerify($uid, $phone, $verify, $type, $id = 0){
		$data = array();
		//数据组织
		$data['uid'] = $uid;
		$data['phone'] = $phone;
		$data['verify'] = $verify;
		$data['create_time'] = $data['update_time'] = time();
		$data['type'] = $type;
		$data['id'] = $id;
		//进行过去验证
		$time = time() - C('SMS_SECOND');
		$map['uid'] = $uid;
		$map['phone'] = $phone;
		$map['status'] = 1;
		$map['create_time'] = array('lt',$time);
		$rs = $this->where($map)->find();
		if($rs){
			//已经存在的信息进行修改
			return $this->where($map)->save($data);
		}else{
			//不存在的进行添加
			return $this->add($data);
		}

	}

	/**
	 * 添加手机验证记录
	 * @param int $uid 用户标识
	 * @param int $phone 手机号码
	 * @param string $verify 验证码
	 * @param int $status 状态
	 * @param int $type 类型
	 * @param int $id 优惠标示
	 * @return int
	 * @author Ice <iceinto@mallog.com.cn>
	 */
	public function updateUidPhoneVerify($uid, $phone, $verify, $status = 2, $type = 1, $id = 0){
		$data = array();
		$map = array();
		$map['uid'] = $uid;
		$map['phone'] = $phone;
		$map['verify'] = $verify;
		$map['type'] = $type;
		$map['id'] = $id;
		$data['update_time'] = $data['verify_time'] = time();
		$data['status'] = $status;
		return $this->where($map)->save($data);
	}
	
	/**
	 * 查看短信发送数量
	 * @param int $phone 手机号码
	 * @param int $uid 用户标示
	 * @param int $type 类型
	 * @return int 数量
	 * @author Ice <iceinto@mallog.com.cn>
	 */
	public function checkDaySendCount($phone, $uid, $type = 1){
		$date = new Date();
		$theDay = $date->format('%Y-%m-%d');
		$date = $date->dateAdd(1, 'd');
		$nextDay = $date->format('%Y-%m-%d');
		$map = array();
		$map['create_time']  = array(array('gt',strtotime($theDay)),array('lt',strtotime($nextDay)),'and'); 
		if($type == 1){
			$map['phone'] = $phone;
		}else if($type == 2){
			$map['uid'] = $uid;
		}else if($type == 3){
			$map['phone'] = $phone;
			$map['uid'] = $uid;
		}
		return $this->where($map)->count();
	}
}